package com.qfmy.web.app.service.impl.listenBook;

import com.qfmy.model.entity.listenBook.AudioChapter;
import com.qfmy.model.vo.listenBook.AudioChapterVo;
import com.qfmy.web.app.mapper.listenBook.AudioChapterMapper;
import com.qfmy.web.app.service.listenBook.AudioChapterService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.listenBook
 * @date 2025/11/01
 * @description 章节表
 */
@Service
public class AudioChapterServiceImpl implements AudioChapterService {

    /**
     * 注入mapper
     */
    @Autowired
    private AudioChapterMapper audioChapterMapper;

    /**
     *
     * @param audioId
     * @return
     */
    @Override
    public List<AudioChapterVo> getAudioChapters(Integer audioId) {
        //根据音频ID查询章节
        List<AudioChapter> audioChapters = audioChapterMapper.getAudioChapters(audioId);
        //转换为VO
        return convertToVo(audioChapters);
    }

    /**
     * 转换为VO
     * @param audioChapters
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<AudioChapterVo> convertToVo(List<AudioChapter> audioChapters) {
        //判断集合是否为空
        if (audioChapters != null && !audioChapters.isEmpty()) {
            return audioChapters.stream().map(audioChapter -> {
                AudioChapterVo audioChapterVo = new AudioChapterVo();
                BeanUtils.copyProperties(audioChapter, audioChapterVo);
                return audioChapterVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
